package Exception;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class demo1 {
    public static void main(String[] args) throws IOException {
        //1.用户输入信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的路径:");
        File rootDir = new File(scanner.next());
        if(!rootDir.isDirectory()){
            System.out.println("输入的目录不存在");
            return;
        }
        System.out.println("请输入关键词:");
        String toDelete = scanner.next();
        //2.遍历目录
        scanDir(rootDir,toDelete);
    }

    private static void scanDir(File rootDir, String toDelete) throws IOException {
        System.out.println("当前访问"+rootDir.getCanonicalPath());
        File[] files = rootDir.listFiles();
        if(files==null){
            return;
        }
        for(File f:files){
            if(f.isDirectory()){
                scanDir(f,toDelete);
            }else{
                checktoDelete(f,toDelete);
            }
        }
    }

    private static void checktoDelete(File f, String toDelete) throws IOException {
        if(f.getName().contains(toDelete)){
            System.out.println("该关键字"+toDelete+"被"+f.getCanonicalPath()+"包含了"+"是否删除?(Y/N)");
            Scanner scanner = new Scanner(System.in);
            String choice = scanner.next();
            if(choice=="Y"||choice=="y"){
                f.delete();
            }
        }
    }
}
